import { useEffect, useState } from "react"
import { reqAllCurriculum, reqClasses, reqClassrooms, reqProfessional, reqTeacher } from "../api/api"

// 班级与课程
export default function useAdditionalInfor(role) {
  const [data, setData] = useState()
  const [classrooms, setClassrooms] = useState()
  const [teacherInfor, setTeacherInfor] = useState(null)
  const [allCurriculum, setAllCurriculum] = useState(null)
  const [professional,setProfessional] = useState()


  const dataInfor = (fun, setMethod,roleInfor)=>{
    return fun(roleInfor).then(
      res => {
        setMethod(res.data)
      }
    )
  }

  useEffect(() => {
    if (role !== null) {
      dataInfor(reqClasses,setData,role);
      dataInfor(reqClassrooms,setClassrooms);
      dataInfor(reqTeacher,setTeacherInfor);
      dataInfor(reqAllCurriculum,setAllCurriculum);
      dataInfor(reqProfessional,setProfessional);
    }
  }, [role])



  const classes = [
    { value: '', label: "" }
  ];
  const curriculum = [
    { value: '', label: "" }
  ];
  const classroom = [
    { value: '', label: "" }
  ];
  const teacher = [
    { value: '', label: "" }
  ];
  const allCurriculunms = [
    { value: '', label: "" }
  ];
  const professionals = [
    { value: '', label: "" }
  ];

  if (role !== null) {

    // 专业班级
    data?.sqlClaResult.map((item) => {
      const option = { value: item.fd_claNum, label: item.fd_claName }
      classes.push(option)
    })
    // 专业课程
    data?.sqlCurResult.map((item) => {
      const option = { value: item.fd_curriculumNum, label: item.fd_curriculumName }
      curriculum.push(option)
    })

    // 教室
    classrooms?.map((item) => {
      const option = { value: item.fd_classroomNum, label: item.fd_classroomName }
      classroom.push(option)
    })

    // 老师
    teacherInfor?.map((item) => {
      const option = { value: item.fd_idNum, label: item.fd_name }
      teacher.push(option)
    })

    // 所有课程
    allCurriculum?.map((item) => {
      const option = { value: item.fd_curriculumNum, label: item.fd_curriculumName }
      allCurriculunms.push(option)
    })

    // 专业
    professional?.map((item)=>{
      const option = { value: item.fd_professionalNum, label: item.fd_professionalName }
      professionals.push(option)
    })

  }

  return { classes, curriculum, classroom, teacher, allCurriculunms,professionals }
}


